package edu.scau.cxq.gp.service.impl;

import com.alibaba.fastjson.JSONArray;
import edu.scau.cxq.gp.service.IWarehouseService;
import edu.scau.cxq.gp.entity.dto.WarehouseSignDTO;
import edu.scau.cxq.gp.entity.Warehouse;
import edu.scau.cxq.gp.mapper.WarehouseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.scau.cxq.gp.entity.vo.WarehouseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 存储仓库信息 服务实现类
 * </p>
 *
 * @author CXQ
 * @since 2022-01-30
 */
@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse> implements IWarehouseService {

    @Resource
    private WarehouseMapper warehouseMapper;

    /**
     * 获取所有的仓库标签
     * @return
     */
    @Override
    public List<WarehouseSignDTO> findWareSigns() {
        return warehouseMapper.selectAllSignOfWarehouse();
    }

    /**
     * 获取仓库状态信息
     * @return
     */
    @Override
    public WarehouseVO getWarehouseInfo() {
        // 查出所有的warehouse
        List<Warehouse> warehouses = warehouseMapper.selectList(null);

        JSONArray warehouseNames = new JSONArray();
        JSONArray frees = new JSONArray();
        JSONArray busy = new JSONArray();

        for (Warehouse warehouse : warehouses) {
            warehouseNames.add(warehouse.getWareName());

            Integer wareFree = warehouse.getWareFree();
            frees.add(wareFree);

            Integer wareContains = warehouse.getWareContains();
            busy.add(wareContains - wareFree);
        }

        WarehouseVO warehouseVO = new WarehouseVO();
        warehouseVO.setWarehouseNames(warehouseNames);
        warehouseVO.setFrees(frees);
        warehouseVO.setBusy(busy);

        return warehouseVO;
    }
}